package org.leiyang.common.dtos;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.leiyang.common.entities.MsgOneToOne;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class Message implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 消息ID，对应与{@link MsgOneToOne#id}
     */
    private Long msgId;

    /**
     * 消息类型
     */
    private MessageType messageType;

    /**
     * 发送消息的用户ID
     */
    private Long fromUserId;

    /**
     * 消息的目标用户ID列表
     */
    private List<Long> toUserIds;

    /**
     * token值
     */
    private String token;

    /**
     * 消息内容
     */
//    private byte[] msgContent;
    private String msgContent;

    /**
     * 消息发送的时间戳
     */
    private long time;

    /**
     * 将{@link MsgOneToOne}转化为{@link Message}
     */
    public static Message convertFromMsgOneToOne(MsgOneToOne msgOneToOne) {
        return Message.builder().msgId(msgOneToOne.getId())
                .messageType(MessageType.get(msgOneToOne.getMsgType()))
                .fromUserId(msgOneToOne.getFromUserId())
                .toUserIds(Collections.singletonList(msgOneToOne.getToUserId()))
                .token("")
                .msgContent(msgOneToOne.getTextMsg())
                .time(msgOneToOne.getTime())
                .build();
    }
}